﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace mstdump_Analyzer
{
    class FilesList : IDisposable
    {
        private DataTable filesListTable = null;
        private long totalFilesSize = 0;
        private int imgCount = 0;
        private int tgzCount = 0;
        private bool KEEP_RUNNING; 
        private string OUTPUT_DIR_PATH;
        //
        //----------------------------------------------------
        //
        public FilesList(ref bool KEEP_RUNNING, ref string OUTPUT_DIR_PATH)
        {
            this.KEEP_RUNNING = KEEP_RUNNING;   
            this.OUTPUT_DIR_PATH = OUTPUT_DIR_PATH;
            // create a DataTable
            filesListTable = new DataTable();
            // add columns
            filesListTable.Columns.Add("File name", typeof(string));
            filesListTable.Columns.Add("Size [bytes]", typeof(long));
        }
        //
        //----------------------------------------------------
        //
        public DataTable getFilesList()
        {
            return filesListTable;
        }
        //
        //----------------------------------------------------
        //
        public long getTotalFilesSize()
        {
            return totalFilesSize;
        }
        //
        //----------------------------------------------------
        //
        public int getImgCount()
        {
            return imgCount;
        }
        //
        //----------------------------------------------------
        //
        public int getTgzCount()
        {
            return tgzCount;
        }
        //
        //----------------------------------------------------
        //
        public void scanInfo()
        {
            string path = Methods.FindFileRec(OUTPUT_DIR_PATH, "list-var_opt_tms.txt");
            if (path != null)
            {
                scanFilesList(path);
            }
        }
        //
        //----------------------------------------------------
        //
        public void scanFilesList(string srcFilePath)
        {
            if (srcFilePath == null)
                return;
            StreamReader sr = new StreamReader(srcFilePath);
            try
            {
                Item tmpItem = new Item();
                while (KEEP_RUNNING && sr.Peek() >= 0)
                {
                    // read line
                    string line = sr.ReadLine();
                    try
                    {
                        tmpItem = new Item();
                        // make sure we have only one space between each to columns
                        line = Methods.RemoveExtraSpaces(line);
                        string []splitted = Regex.Split(line, " ");
                        tmpItem.itemName = splitted[11];
                        tmpItem.content = splitted[7];
                        try
                        {
                            filesListTable.Rows.Add(tmpItem.itemName, long.Parse(tmpItem.content));
                        }
                        catch { }
                        // update total files size
                        totalFilesSize += long.Parse(tmpItem.content);
                        // update imgCount
                        if (tmpItem.itemName.Contains(".img"))
                        {
                            imgCount++;
                        }
                        // update tgzCount
                        if (tmpItem.itemName.Contains(".tgz"))
                        {
                            tgzCount++;
                        }
                    }//try
                    catch
                    { }
                }
            }//try
            catch
            { }
            sr.Dispose();
        }
        //
        //----------------------------------------------------
        //

        public void Dispose()
        {
            if (filesListTable != null)
            {
                filesListTable.Dispose();
            }
        }
    }
}
